﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using ViewRes;
using YuYuKiCms.Bl.Attribute;
using YuYuKiCms.Bl.Interface;
using YuYuKiCms.Framework.Core.Extention;
using YuYuKiCms.Framework.Interface;
using YuYuKiCms.Internet.Properties;
using YuYuKiCms.Model;

namespace YuYuKiCms.Internet.Controllers
{
    [Authorize]
    [DynamicLayout]
    public class AccountController : Controller
    {
        private readonly IAccountBL _accountBL;
        private readonly IUtil _util;

        public AccountController(IAccountBL accountBL, IUtil util)
        {
            _accountBL = accountBL;
            _util = util;
        }

        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl, string confirmationToken)
        {
            if (Request.IsAuthenticated)
            {
                Logoff();
            }

            ViewBag.ReturnUrl = string.IsNullOrEmpty(returnUrl) ? Url.Action("Index", "Home") : returnUrl;
            ViewBag.confirmationToken = confirmationToken;
            return View();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginVm loginVm, string returnUrl, string confirmationToken)
        {
            if (ModelState.IsValid)
            {
                if (_accountBL.Login(loginVm, confirmationToken ?? string.Empty))
                {
                    FormsAuthentication.SetAuthCookie(loginVm.Email, loginVm.RememberMe);

                    if (loginVm.RememberMe)
                    {
                        var cookie = Response.Cookies[FormsAuthentication.FormsCookieName];
                        if (cookie != null)
                            cookie.Expires = DateTime.Now.AddYears(3);
                    }

                    return this.RedirectToLocal(returnUrl);
                }
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", Home.Login_failed);
            return View();
        }

        //
        // GET: /Account/LogOff

        public ActionResult Logoff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Register

        [AllowAnonymous]
        public ActionResult Registration(string returnUrl)
        {
            ViewBag.returnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Registration(string returnUrl, RegistrationVm registrationVm)
        {
            if (ModelState.IsValid)
            {
                var confirmationToken = _accountBL.Registration(registrationVm);
                _util.SendMail(
                    registrationVm.Email, Settings.Default.MAIL_FROM,
                    Account.Activate_your_account,
                    string.Format(Account.Activate_account_msg,
                        Url.Action("Login", "Account",
                            new {confirmationToken}, "http")));

                return this.RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            return View();
        }
    }
}